<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    title="修改">
    <el-form ref="dataForm" :model="dataForm" label-width="80px"
             @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="节点名称" prop="slaveName">
        <el-input v-model="dataForm.slaveName" disabled="true" placeholder="节点名称"></el-input>
      </el-form-item>
      <el-form-item label="ip地址" prop="ip">
        <el-input v-model="dataForm.ip" disabled="true" placeholder="ip地址"></el-input>
      </el-form-item>
      <el-form-item label="hosts信息" prop="hosts">
        <el-input v-model="dataForm.hosts" autosize placeholder="hosts信息" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        slaveId: 0,
        slaveName: '',
        ip: '',
        hosts: '',

      },
    }
  },
  methods: {
    init(slaveId) {
      this.dataForm.slaveId = slaveId || 0
      this.$http({
        url: this.$http.adornUrl('/test/stressHosts/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
        })
      }).then(({data}) => {
        this.roleList = data && data.code === 0 ? data.list : []
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        console.log(this.dataForm.slaveId)
        console.log(this.dataForm)
        if (this.dataForm.slaveId) {
          this.$http({
            url: this.$http.adornUrl(`/test/stressHosts/info/${this.dataForm.slaveId}`),
            method: 'get',
            // params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.slaveName = data.hostEntity.slaveName
              this.dataForm.ip = data.hostEntity.ip
              this.dataForm.hosts = data.hostEntity.hosts
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/test/stressHosts/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'slaveId': this.dataForm.slaveId || undefined,
              'slaveName': this.dataForm.slaveName,
              'ip': this.dataForm.ip,
              'hosts': this.dataForm.hosts,

            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: data.msg,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
